package com.central.utils

import java.sql.{DriverManager, ResultSet}
import java.util.Properties

import com.alibaba.druid.pool.DruidDataSourceFactory
import javax.sql.DataSource

object SaveData {
private val properties = new Properties()
  val url: String = String.valueOf(ConfigUtil.properties.get("mysql.url"))
  val username = String.valueOf(ConfigUtil.properties.get("mysql.username"))
  val password = String.valueOf(ConfigUtil.properties.get("mysql.password"))
  val TABLE_FACE_RELATION: String = ConfigUtil.getProperties().getProperty("mysql.face.table.result")
  val TABLE_CAR_RELATION: String = ConfigUtil.getProperties().getProperty("mysql.car.table.result")
  val dataSource: Option[DataSource] = {
    try {
      val druidProps = ConfigUtil.properties
      // 获取Druid连接池的配置文件
      // 倒入配置文件
      Some(DruidDataSourceFactory.createDataSource(druidProps))
    } catch {
      case error: Exception =>
        //        LOG.error("Error Create Mysql Connection", error)
        None
    }
  }

  def insertResult(key: String, count: String, firsttime: String, lasttime: String, code_type: String): Unit = {
    val table = TABLE_FACE_RELATION
    val face_code = key.split("_")
    val face = face_code(0)
    val code = face_code(1)
    try {
      Class.forName("com.mysql.jdbc.Driver")
    } catch {
      case e =>
        e
    }
    //    val connection = DriverManager.getConnection(url, username, password)
    val connection = dataSource.get.getConnection
    val statement = connection.prepareStatement(s"insert into $table values(?,?,?,?,?,?,?)")
    statement.setString(1, face)
    statement.setString(2, code)
    statement.setInt(3, Integer.parseInt(count))
    statement.setString(4, firsttime)
    statement.setString(5, lasttime)
    statement.setString(6, code_type)
    statement.setDouble(7, 60.00)
    statement.executeUpdate()
    statement.close()
    connection.close()

  }

  def updateResult(data: Array[(String, String, String, String)]): Unit = {
    val table = TABLE_FACE_RELATION
    try {
      //      Class.forName("com.mysql.jdbc.Driver")
      Class.forName("com.mysql.cj.jdbc.Driver")
    } catch {
      case e =>
        e
    }
    val connection = dataSource.get.getConnection

    //    val connection = DriverManager.getConnection(url, username, password)
    connection.setAutoCommit(false)
    val statement = connection.prepareStatement(
      s"update  $table set counts = ?, last_time=? where faceid = ? and tagcode = ? ",
      ResultSet.TYPE_SCROLL_SENSITIVE,
      ResultSet.CONCUR_READ_ONLY
    )

    for (i <- 0 to data.length - 1) {
      val face = data(i)._1.split("_")(0)
      val code = data(i)._1.split("_")(1)
      val count = data(i)._2
      //      println("face:"+face+"  "+code+"    "+count+"========================count")
      val lasttime = data(i)._3

      statement.setInt(1, Integer.parseInt(count))
      statement.setLong(2, ParseTime.DateToLong(lasttime))
      statement.setString(3, face)
      statement.setString(4, code)
      statement.addBatch()
    }

    val ints = statement.executeBatch()

    connection.commit()
    statement.close()
    connection.close()
  }

  def updateResultDistribute(data: (String, String, String, String)): Unit = {
    //此方法在更新目标表时，可能会因为索引导致死锁问题
    val table = TABLE_FACE_RELATION
    try {
      Class.forName("com.mysql.cj.jdbc.Driver")
    } catch {
      case e =>
        e.printStackTrace()
    }
    val connection = dataSource.get.getConnection
    //        val connection = DriverManager.getConnection(url, username, password)
    val statement = connection.prepareStatement(
      s"update  $table set counts = ? where faceid = ? and code = ? "
    )

    val face = data._1.split("_")(0)
    val code = data._1.split("_")(1)
    val count = data._2
    val lasttime = data._3

    statement.setInt(1, Integer.parseInt(count))
//    statement.setLong(2, ParseTime.DateToLong(lasttime))
    statement.setString(2, face)
    statement.setString(3, code)
    val ints = statement.execute()
    statement.close()
    connection.close()
  }

  def updateCarResult(data: Array[(String, String, String, String)]): Unit = {
    var table=TABLE_CAR_RELATION
    try {
      //      Class.forName("com.mysql.jdbc.Driver")
      Class.forName("com.mysql.cj.jdbc.Driver")
    } catch {
      case e =>
        e
    }
    val connection = DriverManager.getConnection(url, username, password)
    connection.setAutoCommit(false)
    val statement = connection.prepareStatement(
      s"update  $table set counts = ? where licenseplateid = ? and tagcode = ? ",
      ResultSet.TYPE_SCROLL_SENSITIVE,
      ResultSet.CONCUR_READ_ONLY
    )

    for (i <- 0 to data.length - 1) {
      val face = data(i)._1.split("_")(0)
      val code = data(i)._1.split("_")(1)
      val count = data(i)._2
      val lasttime = data(i)._3

      statement.setInt(1, Integer.parseInt(count))
//      statement.setLong(2, ParseTime.DateToLong(lasttime))
      statement.setString(2, face)
      statement.setString(3, code)
      statement.addBatch()
    }

    val ints = statement.executeBatch()

    connection.commit()
    statement.close()
    connection.close()
  }

}
